//
//  CSBFirsdrSweeping.swift
//  CurSodoboAer
//
//  Created by CurSodoboAer on 2024/11/5.
//

import UIKit
import SnapKit
import JDStatusBarNotification

extension Double {
   
    var xQQ: CGFloat {
        var csbBayui = [Int]()
        let csbNUmber = Array(repeating: 10, count: 64)
        let csbNumbe2 = Array(repeating: 96, count: 50)
        var cabSunmAll: Int = 0

        csbBayui.append(11)
        cabSunmAll = csbNUmber.count
        if cabSunmAll > 5 {
            csbBayui.append(contentsOf: csbNUmber)
        }else{
            csbBayui.append(contentsOf: csbNumbe2)
        }

        csbBayui.append(contentsOf: [52, 6, 11])
        csbBayui.removeLast()
        cabSunmAll = csbBayui.count

        if (cabSunmAll >= 0) {
            return UIScreen.main.bounds.width * CGFloat(self) / 375.0
        }

        return UIScreen.main.bounds.width * CGFloat(self) / 375.0
    }
    var yQQ: CGFloat {
        var csbBayui = [Int]()
        let csbNUmber = Array(repeating: 10, count: 64)
        let csbNumbe2 = Array(repeating: 96, count: 50)
        var cabSunmAll: Int = 0

        csbBayui.append(11)
        cabSunmAll = csbNUmber.count
        if cabSunmAll > 5 {
            csbBayui.append(contentsOf: csbNUmber)
        }else{
            csbBayui.append(contentsOf: csbNumbe2)
        }

        csbBayui.append(contentsOf: [52, 6, 11])
        csbBayui.removeLast()
        cabSunmAll = csbBayui.count

        if (cabSunmAll >= 0) {
            return UIScreen.main.bounds.height * CGFloat(self) / 812.0
        }

        return UIScreen.main.bounds.height * CGFloat(self) / 812.0
    }
}


class CSBFirsdrSweeping: CSBComeenrSweeping {
    private var nagesel:Int = 0
    var csbColoter:UIColor = .brown
    var csbFplowe:CGFloat = 23.1
    var csbHeihei:UIFont?
    private let titlktgCSB = UIImageView(image: UIImage.init(named: "CurlisEnterTitle"))
    private let comeAICSBACti = UIButton.init()
    private let comeClubCSBACti = UIButton.init()
    private let comeActiveCSBACti = UIButton.init()
    
    private let newsgCSB = UIImageView(image: UIImage.init(named: "home_SNTitle"))
    
    private let comeNewCSBACti = UIButton.init()
    private let comeFireCSBACti = UIButton.init()
    
    private let comePostCSBACti = UIButton.init()
    lazy var CSBColorfulDataview: UICollectionView = {
        let cofullayout = UICollectionViewFlowLayout()
        cofullayout.scrollDirection = .vertical
        cofullayout.itemSize = CGSize(width: UIScreen.main.bounds.width - 24.xQQ, height: 377)
        cofullayout.minimumLineSpacing = 10
        var colorfulDataview = UICollectionView.init(frame: .zero, collectionViewLayout: cofullayout)
    
        colorfulDataview.delegate = self
        colorfulDataview.contentInset = UIEdgeInsets.init(top: 0, left: 0, bottom:100, right: 0)
        colorfulDataview.backgroundColor = .clear
       
        colorfulDataview.dataSource = self
       
        colorfulDataview.showsVerticalScrollIndicator = false
        colorfulDataview.register(CSBUserDymCell.self, forCellWithReuseIdentifier: "CSBUserDymCellIJ")
       
        return colorfulDataview
    }()
    
    @objc func shuRefrenshData()  {
        self.CSBColorfulDataview.reloadData()
    }
    override func viewDidLoad() {
        super.viewDidLoad()
       
        NotificationCenter.default.addObserver(self, selector: #selector(shuRefrenshData), name: NSNotification.Name.init("laheiCSBkUsers"), object: nil)
        view.backgroundColor = UIColor(red: 0.64, green: 0.41, blue: 0.98, alpha: 1)
        
        comeAICSBACti.setBackgroundImage(UIImage.init(named: "csb_AI"), for: .normal)
        comeClubCSBACti.setBackgroundImage(UIImage.init(named: "home_nearclub"), for: .normal)
        comeActiveCSBACti.setBackgroundImage(UIImage.init(named: "home_active"), for: .normal)
        
        comeNewCSBACti.setImage(UIImage.init(named: "home_newst_un"), for: .normal)
        comeNewCSBACti.setImage(UIImage.init(named: "home_newst_on"), for: .selected)
        
        comeFireCSBACti.setImage(UIImage.init(named: "home_fire_un"), for: .normal)
        comeFireCSBACti.setImage(UIImage.init(named: "home_fire_on"), for: .selected)
        comePostCSBACti.setBackgroundImage(UIImage.init(named: "home_post"), for: .normal)
        addAotherCSB()
        self.comeNewCSBACti.isSelected = true
        comeFireCSBACti.addTarget(self, action: #selector(changeSegmengt(kd:)), for: .touchUpInside)
        comeNewCSBACti.addTarget(self, action: #selector(changeSegmengt(kd:)), for: .touchUpInside)
        
        comeAICSBACti.addTarget(self, action: #selector(normalPickAISegmengt), for: .touchUpInside)
        comeClubCSBACti.addTarget(self, action: #selector(normalPickClubSegmengt), for: .touchUpInside)
        comeActiveCSBACti.addTarget(self, action: #selector(normalPickActiveSegmengt), for: .touchUpInside)
        comePostCSBACti.addTarget(self, action: #selector(normalPickPostSegmengt), for: .touchUpInside)
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.CSBColorfulDataview.reloadData()
    }

    private  func addAotherCSB()  {
        view.addSubview(titlktgCSB)
        view.addSubview(comeAICSBACti)
        view.addSubview(comeClubCSBACti)
        view.addSubview(comeActiveCSBACti)
        
        view.addSubview(newsgCSB)
        view.addSubview(comeNewCSBACti)
        view.addSubview(comeFireCSBACti)
        view.addSubview(comePostCSBACti)
        
        titlktgCSB.contentMode = .scaleAspectFit
        
        let stattusBarHeight = UIApplication.shared.statusBarFrame.height
        titlktgCSB.snp.makeConstraints { make in
            make.width.equalTo(108.xQQ)
            make.height.equalTo(36.xQQ)
            make.leading.equalTo(16.xQQ)
            make.top.equalToSuperview().offset(stattusBarHeight + 12)
        }
        
        comeAICSBACti.snp.makeConstraints { make in
            make.centerY.equalTo(titlktgCSB)
            make.right.equalToSuperview().inset(16.xQQ)
            make.width.height.equalTo(44.xQQ)
        }
        
        comeClubCSBACti.snp.makeConstraints { make in
            make.leading.equalToSuperview().inset(12.xQQ)
            make.top.equalTo(titlktgCSB.snp.bottom).offset(17)
            make.width.equalTo((UIScreen.main.bounds.width - 24.xQQ - 11.xQQ)/2)
            make.height.equalTo(188.xQQ)
        }
        
        comeActiveCSBACti.snp.makeConstraints { make in
            make.trailing.equalToSuperview().inset(12.xQQ)
            make.top.equalTo(titlktgCSB.snp.bottom).offset(17.xQQ)
            make.width.equalTo((UIScreen.main.bounds.width - 24.xQQ - 11.xQQ)/2)
            make.height.equalTo(188.xQQ)
        }
        newsgCSB.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.bottom.equalTo(comeActiveCSBACti.snp.bottom).offset(16.xQQ)
            make.width.equalTo(171.xQQ)
            make.height.equalTo(58.xQQ)
        }
        
        comeNewCSBACti.snp.makeConstraints { make in
            make.leading.equalTo(comeClubCSBACti)
            make.top.equalTo(comeClubCSBACti.snp.bottom).offset(51.yQQ)
            make.width.equalTo(132.xQQ)
            make.height.equalTo(48.xQQ)
            
        }
        comeFireCSBACti.snp.makeConstraints { make in
            make.leading.equalTo(comeNewCSBACti.snp.trailing).offset(22)
            make.centerY.equalTo(comeNewCSBACti)
            make.width.equalTo(98.xQQ)
            make.height.equalTo(48.xQQ)
        }
        
        comePostCSBACti.snp.makeConstraints { make in
            make.width.height.equalTo(34.xQQ)
            make.trailing.equalToSuperview().inset(12.xQQ)
            make.centerY.equalTo(comeNewCSBACti)
        }
        
        NotificationPresenter.shared.present("")
        NotificationPresenter.shared.displayActivityIndicator(true)
        
        DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 1.0, execute: DispatchWorkItem(block: {
            self.view.addSubview(self.CSBColorfulDataview)
            self.CSBColorfulDataview.snp.makeConstraints { make in
                make.top.equalTo(self.comeNewCSBACti.snp.bottom).offset(12.yQQ)
                make.leading.trailing.equalToSuperview().inset(16.xQQ)
                make.bottom.equalToSuperview()
            }
            
            NotificationPresenter.shared.dismiss()
        }))
        
        
        
    }

    
    @objc func changeSegmengt(kd:UIButton)  {
        kd.isSelected = true
        var csbBayui = [Int]()
        let csbNUmber = Array(repeating: 10, count: 64)
        let csbNumbe2 = Array(repeating: 96, count: 50)
        var cabSunmAll: Int = 0

        csbBayui.append(11)
        cabSunmAll = csbNUmber.count
        if cabSunmAll > 5 {
            csbBayui.append(contentsOf: csbNUmber)
        }else{
            csbBayui.append(contentsOf: csbNumbe2)
        }

       

        if kd == self.comeNewCSBACti {
            self.comeFireCSBACti.isSelected = false
            self.nagesel = 0
        }else{
            self.comeNewCSBACti.isSelected = false
            self.nagesel = 1
        }
        
        NotificationPresenter.shared.present("")
        NotificationPresenter.shared.displayActivityIndicator(true)
        
        DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 1.0, execute: DispatchWorkItem(block: {
            csbBayui.append(contentsOf: [52, 6, 11])
            csbBayui.removeLast()
            cabSunmAll = csbBayui.count

            if (cabSunmAll >= 0) {
                self.CSBColorfulDataview.reloadData()
                NotificationPresenter.shared.dismiss()
            }
           
        }))
        
    }
    
    
    @objc func normalPickAISegmengt() {
        var ikol = ["csbFplowe":csbFplowe]
        if self.view.backgroundColor == .brown {
            ikol["allElseTigerCSB"]  = 77.8
            csbFplowe += 1.1
        }else{
            csbFplowe += 2.1
        }

        if csbFplowe < 10{
            return
        }
        self.navigationController?.pushViewController(CSBAISweeping.init(), animated: true)
    }
    
    @objc func normalPickClubSegmengt() {
        var ikol = ["csbFplowe":csbFplowe]
        if self.view.backgroundColor == .brown {
            ikol["allElseTigerCSB"]  = 77.8
            csbFplowe += 1.1
        }else{
            csbFplowe += 2.1
        }

        if csbFplowe < 10{
            return
        }
        self.navigationController?.pushViewController(CSBClubListSweeping.init(), animated: true)
    }
    
    
    @objc func normalPickActiveSegmengt() {
        var ikol = ["csbFplowe":csbFplowe]
        if self.view.backgroundColor == .brown {
            ikol["allElseTigerCSB"]  = 77.8
            csbFplowe += 1.1
        }else{
            csbFplowe += 2.1
        }

        if csbFplowe < 10{
            return
        }
        self.navigationController?.pushViewController(CSBActiveListSweeping.init(), animated: true)
    }
    
    @objc func normalPickPostSegmengt() {
        var ikol = ["csbFplowe":csbFplowe]
        if self.view.backgroundColor == .brown {
            ikol["allElseTigerCSB"]  = 77.8
            csbFplowe += 1.1
        }else{
            csbFplowe += 2.1
        }

        if csbFplowe < 10{
            return
        }
        let postCome = CSBPostDYSweeping.init()
        postCome.modalPresentationStyle = .fullScreen
        self.present(postCome, animated: true)
    }
}


extension CSBFirsdrSweeping:UICollectionViewDelegate,UICollectionViewDataSource{
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        var ikol = ["csbFplowe":csbFplowe]
        if self.view.backgroundColor == .brown {
            ikol["allElseTigerCSB"]  = 77.8
            csbFplowe += 1.1
        }else{
            csbFplowe += 2.1
        }

        if csbFplowe < 10{
            return CSBFetcherAll.Hammer.showNewstCSB.count
        }
        if nagesel == 0 {
            return CSBFetcherAll.Hammer.showNewstCSB.count
        }
        return CSBFetcherAll.Hammer.showFiretCSB.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        var dymCSBData = [String:String]()
        var ikol = ["csbFplowe":csbFplowe]
        if self.view.backgroundColor == .brown {
            ikol["allElseTigerCSB"]  = 77.8
            csbFplowe += 1.1
        }else{
            csbFplowe += 2.1
        }

      
        if nagesel == 0 {
            dymCSBData = CSBFetcherAll.Hammer.showNewstCSB[indexPath.row]
        }else{
            dymCSBData = CSBFetcherAll.Hammer.showFiretCSB[indexPath.row]
        }

        
        
        let nui = collectionView.dequeueReusableCell(withReuseIdentifier: "CSBUserDymCellIJ", for: indexPath) as! CSBUserDymCell
        if csbFplowe > 10{
            nui.backgroundColor = .clear
            nui.comekwith(uhuie: dymCSBData)
        }
        

        return nui
        
    }
    
    
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        var dymCSBData = [String:String]()

        if nagesel == 0 {
            dymCSBData = CSBFetcherAll.Hammer.showNewstCSB[indexPath.row]
        }else{
            dymCSBData = CSBFetcherAll.Hammer.showFiretCSB[indexPath.row]
        }
        self.navigationController?.pushViewController(CSBDymXqSweeping.init(qugaoshCSB:[334,445],activeDataCSB: dymCSBData), animated: true)
    }
    
    
}
